package entrance

import (
	"GoServer/common"
	"GoServer/entrance/game/handlers"
	"GoServer/entrance/platform/douyin"
	"GoServer/entrance/platform/kuaishou"
	"GoServer/entrance/rank"
	"GoServer/message"
	"GoServer/net/ws"
	"GoServer/utils/databases"
	"GoServer/utils/debug"
	"GoServer/utils/service"
	"fmt"
	"github.com/spf13/viper"
	"strings"
)

var GameName string = "tuoluo"

func Main(gameName string, port string) {
	databases.InitDatabases()

	parts := strings.SplitN(gameName, "-", 2)
	if len(parts) == 2 {
		if parts[0] == "ks" {
			common.Platform = common.Kuaishou
		}
	} else {
		common.Platform = common.Douyin
	}
	debug.Logln(debug.Debug, fmt.Sprintf("当前启动游戏进程: %s", gameName))
	GameName = gameName
	common.GameName = gameName
	if viper.GetBool("servers.mainProcess") {
		if common.Platform == common.Kuaishou {
			kuaishou.RefreshAccessToken(gameName)

			/// 注册直播任务
			common.RegisterLiveTaskExecutor(&kuaishou.Executor{})
		} else {
			douyin.RefreshAccessToken(gameName)
			rank.BeginRankGame()
			/// 注册消息
			ws.RegisterWSHandler(message.CSStartTask, douyin.OnStartTaskHandler)
			ws.RegisterWSHandler(message.CSCheckTask, douyin.OnCheckTaskHandler)

			/// 注册直播任务
			common.RegisterLiveTaskExecutor(&douyin.Executor{})
		}
	}

	service.Register(port, handlers.RouterForLive)
	service.Register(port, handlers.RouterForUserService)

	// 注册游戏积分
	common.RegisterScoreStrategy(gameName, &common.DefaultGameScore{})

	ws.InitServiceWebsocket() // 初始化websocket
}
